<?php

namespace app\common\model;

use think\Model;


class Couponlog extends Model
{


    // 表名
    protected $name = 'coupon_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
//        'issuance_time_text',
//        'usage_time_text',
//        'status_text'
    ];


    public function getStatusList()
    {
        return ['1' => __('Status 1')];
    }


    public function getIssuanceTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['issuance_time']) ? $data['issuance_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUsageTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['usage_time']) ? $data['usage_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setIssuanceTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUsageTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function coupon()
    {
        return $this->belongsTo(Coupon::class, 'coupon_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    /**
     * Account:优惠卷列表
     * User: Ixiangang
     * Date: 2025/2/8
     * Time: 15:22
     */
    public function getList($where = [], $man_price = '')
    {
        $row = $this->getTermWhere($where);
        $field = $this->getField();
        $order = ['cl.id' => 'desc'];
        $list = $row->alias('cl')
            ->join('coupon c', 'c.id = cl.coupon_id', 'left')
            ->field($field)
            ->order($order)
            ->select();
        collection($list)->each(function ($v, $k) use ($man_price) {
            # 不可使用优惠卷
            if ($man_price && $v['man_price'] >= $man_price){
                $v['use'] = 0;
            }
            # 可以使用优惠卷
            if ($man_price &&  $v['man_price'] <= $man_price ){
                $v['use'] = 1;
            }
        });
        return $list;


    }


    public function getField()
    {
        return "cl.id,cl.coupon_id,cl.use_for,cl.expiration,cl.coupon_price,cl.man_price,
        c.title";
    }


    public function getTermWhere($where)
    {
        return self::where(function ($query) use ($where) {
            if (isset($where['user_id']) && $where['user_id']) {
                $query->where('cl.user_id', '=', $where['user_id']);
            }
            if (isset($where['use_for']) && $where['use_for']) {
                $query->where('cl.use_for', '=', $where['use_for']);
            } else {
                $query->where('cl.use_for', '1');
            }
        });
    }


}
